package zhangtao.bwie.com.yuekao2;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import Bean.LoginBean;
import Presenter.IPresenter;
import Views.IView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, IView{

    private EditText login_zhang;
    private EditText login_pass;
    private TextView log_regi;
    private Button login_btn;
    private IPresenter iPresenter;
    private SharedPreferences user;
    private SharedPreferences.Editor edit;
    private HashMap<String, String> map;
    private LoginBean bean;
    private String zhang;
    private String pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setOnClick();
        user = getSharedPreferences("datauser", Context.MODE_PRIVATE);
        edit = user.edit();
        map = new HashMap<>();
    }

    private void initView() {
        login_zhang = (EditText) findViewById(R.id.login_zhang);
        login_pass = (EditText) findViewById(R.id.login_pass);
        log_regi = (TextView) findViewById(R.id.login_regist_text);
        login_btn = (Button) findViewById(R.id.login_btn);
    }
    public void setPresenter(String url, Map<String,String> map) {
        iPresenter = new IPresenter();
        iPresenter.attach(this);
        iPresenter.getData(url,map);
    }
    private void setOnClick() {
        log_regi.setOnClickListener(this);
        login_btn.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_zhang:
                break;
            case R.id.login_pass:
                break;
            case R.id.login_regist_text:
                Intent intent = new Intent(MainActivity.this,Main2_act.class);
                startActivity(intent);
                break;
            case R.id.login_btn:
                zhang = login_zhang.getText().toString().trim();
                pass = login_pass.getText().toString().trim();
                map.put("mobile", zhang);
                map.put("password", pass);
                setPresenter("http://120.27.23.105/",map);
                break;
        }
    }

    @Override
    public void onSuccess(Object o) {
        if(o instanceof LoginBean) {
            bean = (LoginBean) o;
            Log.d("zzz",bean.toString());
            if(bean.getCode().equals("0")) {
                Intent intent = new Intent(MainActivity.this,ShopData.class);
                startActivity(intent);
                finish();
            }
        }
    }

    @Override
    public void onFailed(Exception e) {

    }
}
